package com.cloudoer.blog.controller;

import com.cloudoer.blog.base.BaseController;
import com.cloudoer.blog.model.Filetype;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/filetype")
public class FiletypeController extends BaseController {

    @RequestMapping(value = "/index")
    public String index (){
        return "filetype/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add (ModelMap modelMap){
        modelMap.put("action", "/filetype/add");
        modelMap.put("filetype", new Filetype());
        return "filetype/add";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update (String id, ModelMap modelMap){
        modelMap.put("action", "/filetype/update");
        modelMap.put("filetype", filetypeService.getById(id));
        return "filetype/add";
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    @ResponseBody
    public Map getAll (){
        List<Filetype> list = filetypeService.getAll();
        return result(list, list.size(), "");
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map getByPage (@RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<Filetype> list = filetypeService.getByPage((pno-1)*psize, psize);
        return result(list, filetypeService.getCount(), "");
    }

    @RequestMapping
    @ResponseBody
    public Filetype getById (String id){
        return filetypeService.getById(id);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map add (Filetype filetype){
        filetypeService.add(filetype);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map update (Filetype filetype){
        filetypeService.update(filetype);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map delete (String id){
        filetypeService.delete(id);
        return result(SUCCESS, "操作成功");
    }
}
